const utils = require("../../module/Utils");
const types = require("../../module/Types");
const ActorPlayingState = types.ActorPlayingState;

cc.Class({
    extends: cc.Component,

    properties: {
        // 所有明牌
        cards: {
            default: [],
            serializable: false,
            visible: false,
        },
        // 暗牌, 暂存
        holeCards: {
            default: null,
            serializable: false,
            visible: false,
        },
        // 手上最接近 21 点的点数（有可能超过 21 点）
        bestPoint: {
            get: function () {
                const minMax = utils.getMinMaxPoint(this.cards);
                return minMax.max;
            }
        },

        // 牌型， 不考虑是否爆牌
        hand: {
            get: function () {
                let count = this.cards.length;
                if (this.holeCards) {
                    ++count;
                }
                if (count >= 5) {
                    return types.Hand.FiveCard;
                }
                if (count === 2 && this.bestPoint === 21) {
                    return types.Hand.BlackJack;
                }
                return types.Hand.Normal;
            }
        },
        canReport: {
            get: function () {
                return this.hand !== types.Hand.Normal;
            },
            visible: false,
        },

        renderer: {
            type: cc.Node,
            default: null,
        },

        state: {
            type: ActorPlayingState,
            default: ActorPlayingState.Normal,
            notify: function (oldState) {
                if (this.state !== oldState) {
                    this.renderer.updateState();
                }
            },
            serializable: false,
        }
    },

    init: function () {
        this.ready = true;
        this.renderer = this.getComponent("actorRenderer");
    },


});
